package com.hqj.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hqj.entity.TMenu;
import com.hqj.service.TMenuServiceImpl;
import com.hqj.util.MenuUtil;
import com.hqj.util.ReturnValueUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hqj
 * @since 2020-06-10
 */
@Controller
@RequestMapping("/muc")
public class TMenuController extends BaseController<TMenu> {

    @Resource(name="TMenuServiceImpl")
    private TMenuServiceImpl msi;

    @Override
    public String index() {
        return "admin/menu/index";
    }

    @Override
    public String addEdit(HttpServletRequest req, Model model) {
        return "admin/menu/add";
    }

    @Override
    public String find(TMenu tMenu) {
        List<TMenu> list = msi.list(null);
        return ReturnValueUtil.findData(list.size(),list);
    }

    @Override
    public Map<String, Object> add(TMenu tMenu) {
        boolean b = msi.save(tMenu);
        return ReturnValueUtil.aer(b,"新增");
    }

    @RequestMapping("/getMenuJson")
    @ResponseBody
    public String getMenuJson(){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.ne("type","btn");
        List<TMenu> list = msi.list(queryWrapper);
        List<Map<String,Object>> result = MenuUtil.createMenuJson(list);
        String json = JSON.toJSONString(result);
        return json;
    }

    @ResponseBody
    @RequestMapping("/getMenuJsonByUserId")
    public String getMenuJsonByUserId(@Param("id") int uid){
        List<TMenu> list = msi.getMenuJsonByUserId(uid);

        List<Map<String,Object>> result = MenuUtil.createMenuJson(list);
        String json = JSON.toJSONString(result);

        return json;
    }


    @RequestMapping("/getMenuJsonAll")
    @ResponseBody
    public String getMenuJsonAll(){
        QueryWrapper queryWrapper = new QueryWrapper();
        List<TMenu> list = msi.list(queryWrapper);
        List<Map<String,Object>> result = MenuUtil.createMenuJson(list);
        String json = JSON.toJSONString(result);
        return json;
    }
}
